function img_out = apply_window(img_in, min_val, max_val)
% APPLY_WINDOW performs manual window-level adjustment
%
%   img_out = apply_window(img_in, min_val, max_val)
%
%   Inputs:
%     - img_in: input grayscale image (any class)
%     - min_val, max_val: window minimum and maximum intensity
%   Output:
%     - img_out: adjusted image scaled to [0,1]

    % Convert to double for processing
    img_in = double(img_in);

    % Apply windowing: clamp and scale
    img_out = (img_in - min_val) / (max_val - min_val);
    img_out(img_out < 0) = 0;
    img_out(img_out > 1) = 1;

    % Convert to single or keep as double depending on need
    img_out = im2single(img_out);
end
